package cn.green.core.front.impl;

import cn.green.core.components.field.FieldAuthTypeCom;
import cn.green.core.components.field.FieldTypeCom;
import cn.green.core.components.store.field.FieldAuthTypeStore;
import cn.green.core.components.store.field.FieldTypeStore;
import cn.green.core.front.QFront;
import cn.green.core.front.model.Fetch;
import cn.green.core.front.model.Input;
import cn.green.core.front.model.Title;
import cn.green.core.model.QField;
import cn.green.core.model.QTable;
import cn.green.manage.cache.QCache;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;

import java.util.List;

/**
 * @author wang.yishu
 * @version 1.0
 * @className DefaultQFront
 * @description 默认前端建造组件
 * @date 2025/2/14 14:52
 */
@Getter
public class DefaultQFront implements QFront {

    private final QCache qCache;

    public DefaultQFront(QCache qCache) {
        this.qCache = qCache;
    }
    @Override
    public JSONArray buildTitle(String source, String schema, String tableName) {
        JSONArray array = new JSONArray();
        List<QField> fieldsFromCache = qCache.getFieldsFromCache(source, schema, tableName);
        if (fieldsFromCache != null && !fieldsFromCache.isEmpty()) {
            fieldsFromCache.sort((o1, o2) -> {
                int o1Sort = o1.getSort() == null ? 0 : o1.getSort();
                int o2Sort = o2.getSort() == null ? 0 : o2.getSort();
                return o1Sort - o2Sort;
            });
            for (QField qField : fieldsFromCache) {
                String fieldAuth = qField.getFieldAuth();
                FieldAuthTypeCom fieldAuthTypeCom = FieldAuthTypeStore.get(fieldAuth);
                if (fieldAuthTypeCom != null) {
                    if (fieldAuthTypeCom.passSelect(qField)) {
                        String fieldType = qField.getFieldType();
                        FieldTypeCom fieldTypeCom = FieldTypeStore.get(fieldType);
                        if (fieldTypeCom != null) {
                            List<Title> titles = fieldTypeCom.titles(qField);
                            if (titles != null && !titles.isEmpty()) {
                                for (Title title : titles) {
                                    JSONObject titleJson = new JSONObject();
                                    titleJson.put("title", title.getTitleName());
                                    titleJson.put("dataIndex", title.getDataIndex());
                                    titleJson.put("key", title.getDataIndex());
                                    array.add(titleJson);
                                }
                            }
                        }
                    }
                }
            }
        }
        return array;
    }

    @Override
    public JSONObject buildFormInsert(String source, String schema, String tableName) {
        JSONObject form = new JSONObject();
        List<QField> fieldsFromCache = qCache.getFieldsFromCache(source, schema, tableName);

        if (fieldsFromCache != null && !fieldsFromCache.isEmpty()) {
            fieldsFromCache.sort((o1, o2) -> {
                int o1Sort = o1.getSort() == null ? 0 : o1.getSort();
                int o2Sort = o2.getSort() == null ? 0 : o2.getSort();
                return o1Sort - o2Sort;
            });
            for (QField qField : fieldsFromCache) {
                String fieldAuth = qField.getFieldAuth();
                FieldAuthTypeCom fieldAuthTypeCom = FieldAuthTypeStore.get(fieldAuth);
                if (fieldAuthTypeCom != null) {
                    if (fieldAuthTypeCom.hasInput(qField)) {
                        String fieldType = qField.getFieldType();
                        FieldTypeCom fieldTypeCom = FieldTypeStore.get(fieldType);
                        if (fieldTypeCom != null) {
                            List<Input> inputs = fieldTypeCom.inputs(qField);
                            if (inputs != null && !inputs.isEmpty()) {
                                for (Input input : inputs) {
                                    String fieldName = input.getFieldName();
                                    form.put(fieldName, input.toFront());
                                }
                            }
                        }
                    }
                }
            }
        }
        return form;
    }

    @Override
    public JSONObject buildFormUpdate(String source, String schema, String tableName) {
        JSONObject form = new JSONObject();
        List<QField> fieldsFromCache = qCache.getFieldsFromCache(source, schema, tableName);
        if (fieldsFromCache != null && !fieldsFromCache.isEmpty()) {
            fieldsFromCache.sort((o1, o2) -> {
                int o1Sort = o1.getSort() == null ? 0 : o1.getSort();
                int o2Sort = o2.getSort() == null ? 0 : o2.getSort();
                return o1Sort - o2Sort;
            });
            for (QField qField : fieldsFromCache) {
                String fieldAuth = qField.getFieldAuth();
                FieldAuthTypeCom fieldAuthTypeCom = FieldAuthTypeStore.get(fieldAuth);
                if (fieldAuthTypeCom != null) {
                    if (fieldAuthTypeCom.hasInput(qField)) {
                        boolean readonly = fieldAuthTypeCom.readonly(qField);
                        String fieldType = qField.getFieldType();
                        FieldTypeCom fieldTypeCom = FieldTypeStore.get(fieldType);
                        if (fieldTypeCom != null) {
                            List<Input> inputs = fieldTypeCom.inputs(qField);
                            if (inputs != null && !inputs.isEmpty()) {
                                for (Input input : inputs) {
                                    String fieldName = input.getFieldName();
                                    input.setReadOnly(readonly);
                                    form.put(fieldName, input.toFront());
                                }
                            }
                        }
                    }
                }
            }
        }
        return form;
    }

    @Override
    public JSONObject buildFormSelect(String source, String schema, String tableName) {
        return this.qCache.getConditionFromCache(source, schema, tableName);
    }

    @Override
    public JSONArray buildConditionCandidate(String source, String schema, String tableName) {
        JSONArray candidates = new JSONArray();
        QTable table = this.qCache.getTableFromCache(source, schema, tableName);
        if (table == null) {
            return new JSONArray();
        }
        List<QField> fields = this.qCache.getFieldsFromCache(source, schema,tableName);
        if (fields != null && !fields.isEmpty()) {
            for (QField field : fields) {
//                if (!field.isCanBeUsedForCondition()) {
//                    continue;
//                }
                field.setTableName(tableName);
                String fieldType = field.getFieldType();
                if (fieldType != null && !fieldType.isEmpty()) {
                    FieldTypeCom fieldTypeCom = FieldTypeStore.get(fieldType);
                    if(fieldTypeCom != null) {
                        List<Fetch> fetches = fieldTypeCom.fetches(table, field);
                        if (fetches != null) {
                            for (Fetch fetch : fetches) {
                                candidates.add(fetch.toFront());
                            }
                        }
                    }
                }
            }
        }
        return candidates;
    }
}
